package com.preguicoso.client.estabelecimento.cardapio.modelosItens;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.Image;
import com.preguicoso.client.estabelecimento.cardapio.CardapioService;
import com.preguicoso.client.estabelecimento.cardapio.CardapioServiceAsync;
import com.preguicoso.client.estabelecimento.carrinho.Carrinho;
import com.preguicoso.client.estabelecimento.carrinho.CarrinhoPedidosUI;
import com.preguicoso.client.telaInicial.lightBox;
import com.preguicoso.shared.entities.ItemCardapioBean;
import com.preguicoso.shared.utils.MoneyUtils;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;

public class Tipo extends Composite  {

	private static CardapioItemDescricaoUiBinder uiBinder = GWT
			.create(CardapioItemDescricaoUiBinder.class);
	@UiField Image image;
	@UiField Label nome;
	@UiField Label de;
	@UiField Label por;
	@UiField Label descricao;
	@UiField Button adicionar;
	@UiField Button add;
	@UiField Button remove;
	@UiField Label qtdem;
	@UiField Label qtd;
	@UiField TextArea observacao;
	
	ItemCardapioBean i;


	interface CardapioItemDescricaoUiBinder extends
			UiBinder<Widget, Tipo> {
	}
	

	private final CardapioServiceAsync cardapioService = GWT
			.create(CardapioService.class);

	public Tipo() {
		initWidget(uiBinder.createAndBindUi(this));
	}
	public Tipo(ItemCardapioBean i) {
		initWidget(uiBinder.createAndBindUi(this));
		image.setUrl(i.getImagem());
		nome.setText(i.getNome());
		de.setText("");
		por.setText("R$ "+MoneyUtils.parseString(i.getPreco()));
		descricao.setText(i.getDescricao());
		this.i = i;
	}
	@UiHandler("remove")
	void onRemoveClick(ClickEvent event) {
		int qtd = Integer.parseInt(this.qtd.getText());
		if(qtd>1){
			qtd--;
			this.qtd.setText(qtd+"");
		}
	}
	@UiHandler("add")
	void onAddClick(ClickEvent event) {
		int qtd = Integer.parseInt(this.qtd.getText());
		qtd++;
		this.qtd.setText(qtd+"");
	}
	@UiHandler("adicionar")
	void onAdicionarClick(ClickEvent event) {
		adicionar.setEnabled(false);
		i.setQuantidade(Integer.parseInt(qtd.getText()));
		i.setObservacao(observacao.getText());
		Carrinho.getInstance().addItem(i);
		Carrinho.getInstance().refreshCarrinho();
		lightBox.getInstance().hide();	
		if(Window.getClientWidth()<500){
			if(Cookies.getCookie("mesa")!=null && Cookies.getCookie("mesaConfirmada")!=null){
				if(Window.confirm("Pedir para a mesa "+Cookies.getCookie("mesa"))){
					Carrinho.getInstance().pedirMesa(Cookies.getCookie("mesa"));
				}
				else{
					History.newItem(History.getToken()+"/checkout");
				}
			}
			else{
				History.newItem(History.getToken()+"/checkout");
			}
		}
	}
}
